package com.sufen.gb.service.impl;

import com.sufen.common.enums.DateTypeEnum;
import com.sufen.common.reponse.CommonEntity;
import com.sufen.common.utils.DateTimeUtilsD;
import com.sufen.gb.dao.Gb2HzyMapper;
import com.sufen.gb.service.Gb2HzyService;
import com.sufen.common.utils.CollectionUtilsD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @description:
 * @function:
 * @author: Liu Menglei
 * @Date: 2021/12/3 0003 22:23
 */
@Service
public class Gb2HzyServiceImpl implements Gb2HzyService {

    private static final Logger logger = LoggerFactory.getLogger(Gb2HzyServiceImpl.class);
    @Autowired
    Gb2HzyMapper gb2HzyMapper;
    /**
     *@description: 查询数据——单据类型过滤——单据日期处理——无效信息移除
     *@params: 拉应付单据发票相关信息的查询条件
     *@returns: 符合期望的发票信息
     */
    @Override
    public List<CommonEntity> getPayItemsByCon(List<String> conditions) {
        logger.info("开始执行getPayItemsByCon层，条件为："+conditions.toString());
        String[] targetItems = {"billdate","src_tradetype","paymentday"};
        List<CommonEntity> commonEntityList = gb2HzyMapper.getPayItemsByCon(conditions);
        System.out.println(commonEntityList.toString());
        //如果要是查询到的数据为空则将结果直接返回不做处理
        if (CollectionUtils.isEmpty(commonEntityList))return commonEntityList;
        List<CommonEntity> resultLists = (List<CommonEntity>) commonEntityList.stream()
                .filter(new Predicate<CommonEntity>() {
                    //过滤掉src_tradetype为null的单据
                    @Override
                    public boolean test(CommonEntity commonEntity) {
                        Object var = commonEntity.get("src_tradetype");
                        if (var!=null){
                            return true;
                        }return false;
                    }
                })
                .map(item->{
                    //单据日期的业务逻辑计算与无效数据过滤
                    CommonEntity newEntity = item;
                    String var1 = "0";
                    if (!StringUtils.isEmpty(item.get("paymentday"))) {
                        BigDecimal var2 = (BigDecimal) item.get("paymentday");
                         var1 = var2.toString();
                        System.out.println("协议日期不等于null"+var1);
                    }
                        String resultDate = "";
                        String billDate = (String) item.get("billdate");
                        if (!StringUtils.isEmpty(billDate)) {

//                            String paymentday = item.get("paymentday").toString();
                            int resultDay = Integer.parseInt(StringUtils.isEmpty(var1) || var1 == "" ? "0" : var1);
                            resultDate = DateTimeUtilsD.dateFloatDay(billDate, resultDay, DateTypeEnum.DATE_DEFAULT);
                        }
                        newEntity.put("actualMaturityDate", resultDate);

                    int weekday = dateToWeek(resultDate);
                    Object var = newEntity.get("src_tradetype");

                    if (var.equals("F1-Cxx-01")||var.equals("F1-Cxx-11")){


                    } else if (var.equals("F1-Cxx-05")){

                    } else if (var.equals("F1-Cxx-07")){

                    } else {

                    }



                    if (!StringUtils.isEmpty(item.get("amount"))) {
                            String amount = item.get("amount").toString();
                            if (amount.equals("0")) {
                                newEntity.put("isPayment", true);
                            } else {
                                newEntity.put("isPayment", false);
                            }
                        }

                    CollectionUtilsD.mapRemoveByKeys(newEntity,targetItems);
                    return newEntity;
                }).collect(Collectors.toList());
        return resultLists;
    }

    //处理时间的格式
    private String dateUtils(String oldDate,int day){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            Date date = simpleDateFormat.parse(oldDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, day);
            time = calendar.getTime();
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return simpleDateFormat.format(time);
    }

    private int dateToWeek(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        Date date;
        try {
            date = sdf.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return w;
    }

    @Override
    public int getPayItemsByDates(List<HashMap<String, Object>> conditions) {
        logger.info("开始执行getPayItemsByDates层，条件为："+conditions.toString());
        int payItemsByDates = gb2HzyMapper.getPayItemsByDates(conditions);
        if (payItemsByDates == 1){
            return payItemsByDates;
        }else {
            return 0;
        }
    }
}